import React, { Component } from "react";
import "./openFail.scss";
import { connect } from "react-redux";
import XFButton from "@component/button/index";

import XFModal from "../../../xfCompontent/xfModal/xfModal";
import sumitSuccess from "../../../images/imageXHF/sumitSuccess.png";
import { InterfaceG } from "../../../config/Config";
import yhTools from "../../../tools/yh";
import { Toast } from "antd-mobile";
import MainLayout from "../../../component/mainLayout/mainLayout";
export class OpenSuccess extends Component {
	constructor(props) {
		super(props);
		this.state = {
			btnText: "返回首页",
			isShowModal: false,
			backPath: ""
		};
	}

	componentDidMount() {
		this.credit_queryCreditApplyStatus();
	}

	credit_queryCreditApplyStatus = () => {
		const objs = {
			url: InterfaceG.credit_queryCreditApplyStatus,
			params: { pruductNum: "601" }
		};
		yhTools.request(objs).then(res => {
			Toast.hide();
			if (res.code === "0") {
				if (res.data) {
					const { applyStatus } = res.data;
					//0未授信，1已授信，2授信中，4授信拒绝
					let pathName = "";
					if (applyStatus === "0") {
						pathName = "/xhfHomeNoCredit";
					} else if (applyStatus === "1") {
						pathName = "/xhfHomeCredit";
					} else if (applyStatus === "2") {
						pathName = "/xhfHomeCrediting";
					} else if (applyStatus === "4") {
						pathName = "/xhfHomeNoCredit";
					}
					const fromUrl = this.props.location.state
						? this.props.location.state.fromUrl
						: "";
					const lnid = this.props.getAppPlatformsDatas.lnid;
					if (lnid == "601" || lnid == "602") {
						this.setState({
							backPath: pathName
						});
						if (
							fromUrl !== "xhfHomeCredit" ||
							fromUrl !== "xhfHomeNoCredit" ||
							fromUrl !== "xhfHomeCrediting"
						) {
							this.xhfAppSubApply();
						}
					} else if (lnid == "802") {
						if (fromUrl !== "xhdHome") {
							this.xhdAppSubApply();
						}
						this.setState({
							backPath: "/xhdHome"
						});
					} else if (yhTools.isXiaoHuiFu()) {
						this.setState({
							backPath: pathName
						});
					}
					this.setState({
						pathName
					});
				}
			} else {
				Toast.info(res.message, 2);
			}
		});
	};

	xhfAppSubApply() {
		let objs = {
			url: InterfaceG.yhbXhfApply,
			params: {
				productNum: "601",
				userDeviceInfo: JSON.stringify(
					this.props.getAppPlatformsDatas.devicesInfo
				)
			}
		};
		let _that = this;
		yhTools.request(objs).then(res => {
			if (res.code === "0") {
				const status = res.data.status;
				if (status == "1") {
					_that.props.history.push({
						pathname: "/xhfOpenResult",
						state: res.data
					});
				} else if (status == "4") {
					_that.props.history.push({
						pathname: "/xhfOpenFail",
						state: { message: res.data.message }
					});
				}
			} else {
				Toast.info(res.message, 2);
			}
		});
	}
	xhdAppSubApply() {
		let objs = {
			url: InterfaceG.xhdyhApply,
			params: {
				channelCode: "YHB",
				lnid: this.props.getAppPlatformsDatas.lnid,
				applyNo: this.props.getAppPlatformsDatas.applyNo,
				userDeviceInfo: JSON.stringify(
					this.props.getAppPlatformsDatas.devicesInfo
				)
			}
		};
		let _that = this;
		yhTools.request(objs).then(res => {
			if (res.code === "0") {
				const status = res.data.status;
				if (status == "1") {
					_that.props.history.push({
						pathname: "/xhdHome",
						state: res.data
					});
				} else if (status == "4") {
					_that.props.history.push({
						pathname: "/xhfOpenFail",
						state: { message: res.data.message }
					});
				}
			} else {
				Toast.info(res.message, 2);
			}
		});
	}
	btnFn() {
		const { pathName } = this.state;
		if (yhTools.isXiaoHuiFu()) {
			this.props.history.push(pathName);
		} else if (yhTools.isXiaoHuiDai()) {
			this.props.history.push("/xhdHome");
		}
	}

	handleModal() {
		this.setState({
			isShowModal: true
		});
	}

	cancleModal() {
		this.setState({
			isShowModal: false
		});
	}

	gotoOpened() {}

	handleModalBtn() {
		this.setState({
			isShowModal: false
		});
	}

	render() {
		const { btnText = "", isShowModal } = this.state;
		let _pageParams = {
			header: {
				title:
					this.props.getAppPlatformsDatas.lnid == "602"
						? "等待审核"
						: "开通结果",
				goBackPath: this.state.backPath
			},
			history: this.props.history,
			cacheLifecycles: this.props.cacheLifecycles
		};
		return (
			<MainLayout pageParams={_pageParams}>
				<div className="openSuccess">
					<div className="openFail-box">
						<img src={sumitSuccess} />
						<h3>资料提交成功</h3>
						{this.props.getAppPlatformsDatas.lnid == "602" ? (
							<p>
								审核通过我们将会立即告知您审核结果，请注意查看。您也可以通过首页查看审核进度
							</p>
						) : (
							<p>
								最快3分钟出审核结果，请注意查看短信通知。您也可以通过首页查看审核进度。
							</p>
						)}
					</div>
					<XFButton onClick={this.btnFn.bind(this)}>{btnText}</XFButton>
					<XFModal
						handleBtn={this.handleModalBtn.bind(this)}
						isShowModal={isShowModal}
					/>
				</div>
			</MainLayout>
		);
	}
}

function mapStateToProps(state) {
	return state;
}

function mapDispatchToProps() {
	return {};
}

const OpenSuccessPage = connect(
	mapStateToProps,
	mapDispatchToProps
)(OpenSuccess);
export default OpenSuccessPage;
